package org.toy.maxent;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import opennlp.maxent.BasicContextGenerator;
import opennlp.maxent.ContextGenerator;
import opennlp.maxent.DataStream;
import opennlp.maxent.Event;
import opennlp.maxent.EventStream;

/**
 * 
 * @author weip.pengw
 * @time 2010-9-1 下午08:46:50
 */
public class XmlEventStream implements EventStream {

	//private ContextGenerator cg = new MultiFieldContextGenerator();
	private ContextGenerator cg = new ChineseMultiFieldContextGenerator();
	private DataStream ds;
	private Event next;

	public XmlEventStream(DataStream ds) {

		this.ds = ds;
		if (this.ds.hasNext())
			next = createEvent((String) this.ds.nextToken());

	}

	/**
	 * @see opennlp.maxent.EventStream#nextEvent()
	 */
	public Event nextEvent() {
		while (next == null && this.ds.hasNext())
			next = createEvent((String) this.ds.nextToken());

		Event current = next;
		if (this.ds.hasNext()) {
			next = createEvent((String) this.ds.nextToken());
		} else {
			next = null;
		}
		return current;
	}

	/**
	 * 
	 * @see opennlp.maxent.EventStream#hasNext()
	 */
	public boolean hasNext() {
		while (next == null && ds.hasNext())
			next = createEvent((String) ds.nextToken());
		return next != null;
	}

	/**
	 * 
	 * @author weip.pengw
	 * @time 2010-9-1 下午08:52:38
	 * @param obs
	 * @return
	 */
	private Event createEvent(String obs) {

		List<String> tagList = MultiFieldContextGeneratorSupport
				.toContextList(obs);
		if (tagList.size() < 1)
			return null;

		String result = tagList.get(tagList.size() - 1);
		tagList.remove(tagList.size() - 1);
		
		String[] contexts=cg.getContext(tagList);
		if(contexts.length==0){
			System.out.println("drop event:"+obs);
			return null;
		}

		return new Event(result, contexts);

	}

	public static void main(String[] args) {
		DataStream ds = new DataStream() {
			public Object nextToken() {
				return "<id:801725641><DETAIL:  &nbsp;&nbsp;&nbsp; please remove me from your e-mail list.&nbsp;&nbsp;  wajmajgram@verizon.net      &nbsp;   &nbsp;&nbsp;&nbsp; t><IS_SPAM:N>";
			}

			public boolean hasNext() {
				return true;
			}
		};
		XmlEventStream xes = new XmlEventStream(ds);

		System.out.println(xes.next);

	}

}
